﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_NewsView : System.Web.UI.Page
{
    public string Titles;
    public string TypeId;
    public string Contents;
    public string names;
    public string Pubdate;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            names = useram();
            TypeId = TypeName();
            int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "select * from News where NewsID="+NewsID+" ";
            SqlCommand com = new SqlCommand(sql,con);
            SqlDataReader dr = com.ExecuteReader();
            if (dr.Read())
            {
                Titles = dr["title"].ToString();
                Contents = dr["Content"].ToString();
                Pubdate=dr["Pubdate"].ToString();
            }
            con.Close();
        }
    }
    public string useram()
    {
        string Re="";
        int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select Name from Employee where Username=(select username from News where NewsID=" + NewsID + ")";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            Re = dr["Name"].ToString();
        }
        return Re;

    }
    public string TypeName()
    {


        string Re="";
        int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select type from NewsType where NTID=(select TypeId from news where NewsID=" + NewsID + ")";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        return  Re = dr["type"].ToString();

    }
    protected void bnSave_ServerClick(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
        {
            int b = Convert.ToInt32(Request["NewsID"].ToString());
            string a = "署名";
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            Response.Write("<script>alert('发布成功！！');window.location.href='RemarkList.aspx?NewsID=" + b + "';</script>");
            con.Close();
        }
        else
        {
            int b = Convert.ToInt32(Request["NewsID"].ToString());
            string a = Session["UserName"].ToString();
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            Response.Write("<script>alert('发布成功！！');window.location.href='RemarkList.aspx?NewsID=" + b + "';</script>");
            con.Close();
        }
 
    }
    public string Name(string y)
    {

     
        string Re="";
        int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select Name from Employee where username='" + y + "'";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            Re = dr["Name"].ToString();
        }
        return Re;
       
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
        Response.Redirect("RemarkList.aspx?NewsID="+NewsID+"");

    }
}
